﻿using LuShanTea.Front.Web.App_Start;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Front.Web.App_Start
{
    public class CheckLoginFilter : IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            IsLoginAttribute[] attrs = (IsLoginAttribute[])filterContext.ActionDescriptor.GetCustomAttributes(typeof(IsLoginAttribute), false);
            if (attrs.Length <= 0)
            {
                return;
            }
            if (filterContext.HttpContext.Session["USERID"] == null)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonNetResult
                    {
                        Data = new AjaxResult { Status = "redirect", ErrorMsg = "您还没有登录, 即将跳转到登录页面。", Data = "/Login/Index" }
                    };
                    return;
                }
                else
                {
                    filterContext.Result = new RedirectResult("/Login/Index");
                    return;
                }
            }
        }
    }
}